package com.cqtczh.qhd.driver.acticity.carteam;

import httptools.HttpJsonTool;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javabeen.CarTeamInfoBeen;

import org.json.JSONObject;

import tools.L;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.cqtczh.qhd.driver.AppContent;
import com.cqtczh.qhd.driver.R;
import com.cqtczh.qhd.driver.acticity.base.BaseActivity;
import com.cqtczh.qhd.driver.api.GagApi;
import com.cqtczh.qhd.driver.model.CarTeam;
import com.cqtczh.qhd.driver.vo.JsonRequest;
import com.cqtczh.qhd.driver.vo.JsonResponse;
import com.cqtczh.qhd.driver.vo.State;

public class CarTeamRequestInfo extends BaseActivity implements OnClickListener {

	private TextView CarTeamName, CarTeamBelonComplete, CarTeamLeadName,
			CarTeamLeadPhone;
	private String Getid;
	private String car_team_Id;
	private CarTeam carTeam;
	private Button agree;
	private Button reject;
	private int messageID;
	private int pont;
	private List<CarTeamInfoBeen> carTeamInfo = new ArrayList<CarTeamInfoBeen>();

	@SuppressWarnings("null")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_car_team_info);
		Intent intent = getIntent();
		carTeamInfo = new ArrayList<CarTeamInfoBeen>();
		if (intent == null) {
			Getid = "";
		} else {

			Getid = intent.getStringExtra("CarTeamLeadr"); 
			car_team_Id = intent.getStringExtra("CarTeamId"); 
		}
		
		GetCarTeamInfo();
	}

	private void Ini() {
		dismissLoadingDialog();
		TextView head=(TextView) findViewById(R.id.head_title);
		head.setVisibility(View.VISIBLE);
		head.setText("车队信息");
		ImageView imageView=(ImageView) findViewById(R.id.head_left);
		imageView.setVisibility(View.VISIBLE);
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				CarTeamRequestInfo.this.finish();
			}
		});
		pont = getIntent().getIntExtra("Point", -1);
		Button  button=(Button) findViewById(R.id.chek);
		button.setVisibility(View.GONE);
		CarTeamName = (TextView) findViewById(R.id.textView1);
		CarTeamBelonComplete = (TextView) findViewById(R.id.textView2);
		CarTeamLeadName = (TextView) findViewById(R.id.textView3);
		CarTeamLeadPhone = (TextView) findViewById(R.id.textView4);
		TextView ctextVew2=(TextView) findViewById(R.id.ctextVew2);
		ctextVew2.setText("车队描述：");
		agree = (Button) findViewById(R.id.arrge);
		agree.setVisibility(View.VISIBLE);
		reject = (Button) findViewById(R.id.reg);
		reject.setVisibility(View.VISIBLE);
		agree.setOnClickListener(this);
		reject.setOnClickListener(this);
		messageID = getIntent().getIntExtra("MessageId", 0);
	}

	// 获得车队信息
	private void GetCarTeamInfo() {
		showLoadingDialog("加载中...");
		if (Getid != null && (!Getid.equals(""))) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("leaderid", Getid);
			HttpJsonTool httpJsonTool = new HttpJsonTool(
					CarTeamRequestInfo.this);
			httpJsonTool.SetOnLister(new Handler() {
				@Override
				public void handleMessage(Message msg) {
					// TODO Auto-generated method stub
					super.handleMessage(msg);
					if (msg.what == 1) {
//						Toast.makeText(CarTeamRequestInfo.this, "获取到车辆信息",
//								Toast.LENGTH_LONG).show();
						carTeamInfo = (List<CarTeamInfoBeen>) msg.obj;
						
						Ini();
						IniView();
					}else {
						dismissLoadingDialog();
					}
				}
			});
			Type typ = new TypeToken<List<CarTeamInfoBeen>>() {
			}.getType();
			httpJsonTool.StartWork(map, GagApi.GET_TEAM_INFO, typ, null);

		}
	}

	private void IniView() {
		if (!carTeamInfo.isEmpty()) {
			TextView textView1 = (TextView) findViewById(R.id.ctextVew1);

			TextView textView3 = (TextView) findViewById(R.id.ctextVew2);
			TextView textView4 = (TextView) findViewById(R.id.ctextVew3);
			TextView textView5 = (TextView) findViewById(R.id.ctextVew4);
//			L.v(tag, msg);
//			L.v(tag, msg);
//			L.v(tag, msg);
//			L.v(tag, msg);
//			L.v(tag, msg);
			
			textView1.setText("车队名字：" + carTeamInfo.get(0).getMotorcadename());
			if ((carTeamInfo.get(0).getMotorcadedescription()!=null)&&(!carTeamInfo.get(0).getMotorcadedescription().equals(""))) {
				textView3.setText("车队描述："
						+ carTeamInfo.get(0).getMotorcadedescription());
			}else {
				View view=findViewById(R.id.carteam_detail);
				view.setVisibility(View.GONE);
			}
		
			if ((carTeamInfo.get(0).getCarownername()!=null)&&(!carTeamInfo.get(0).getCarownername().equals(""))) {
				textView4.setText("队长名字：" + carTeamInfo.get(0).getCarownername());
			}else {
				textView4.setText("队长名字：" + "队长未填写");
			}			
			textView5.setText("队长电话号码：" + carTeamInfo.get(0).getPhone());
		//	textView1.setText("车队名字：" + carTeamInfo.get(0).getMotorcadeid());

		}
	}

	private void UpDriveridea(String carteamId, final String idea) {
		// httpJsonTool.
		
		AppContent app = (AppContent) getApplication();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("motorcadeid", carteamId);
		L.v("motorcadeid", "" + carteamId);
		map.put("userId", app.userId);
		map.put("carownerid", carTeamInfo.get(0).getCarownerid());
		L.v("carownerid", "" + carTeamInfo.get(0).getCarownerid());
		map.put("state", idea);
		HttpJsonTool httpJsonTool = new HttpJsonTool(this);
		httpJsonTool.SetOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {
					if (idea.equals("yes")) {
						Toast.makeText(CarTeamRequestInfo.this, "加入成功",
								Toast.LENGTH_LONG).show();
						CarTeamRequestInfo.this.finish();
					}
					if (idea.equals("no")) {
						Toast.makeText(CarTeamRequestInfo.this, "已经拒绝",
								Toast.LENGTH_LONG).show();
						CarTeamRequestInfo.this.finish();
					}
				}else {
					Toast.makeText(CarTeamRequestInfo.this, (String)msg.obj, Toast.LENGTH_LONG).show();
				}
			}
		});
		httpJsonTool.StartWork(map,// "http://192.168.1.111:8080/cqtczh/moduls/fleets/receive.do"
				GagApi.DRIVEIR_AGGREEEN_INFO, null, null);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.arrge:
			UpDriveridea(car_team_Id, "yes");
			// Toast.makeText(this, "成功加入车队", Toast.LENGTH_LONG).show();
			break;
		case R.id.reg:
			UpDriveridea(car_team_Id, "no");
			this.finish();
			break;
		default:
			break;
		}
	}

}
